'use client';

import '@/app/styles/data-table.scss';
import DataTable, { DataTableColumn } from '@/components/table/DataTable';
import { CbExerciseRow } from '@/types/seibro';
import { formatDecimal, formatShares, dateOrDash } from '@/lib/utils/market';

function buildColumns(): DataTableColumn<CbExerciseRow>[] {
	return [
		{
			key: 'rgtStdDt',
			header: '행사일',
			align: 'left',
			priority: 'high',
			cell: (row) => row.rgtStdDt
		},
		{
			key: 'bond',
			header: '채권',
			align: 'left',
			priority: 'high',
			cellClassName: 'data-table__cell--name',
			cell: (row) => (
				<span className='stock-cell'>
					<span className='stock-cell__name'>{row.bondName ?? row.bondIsin}</span>
					{row.bondKind && <span className='market__cls-badge'>{row.bondKind}</span>}
				</span>
			)
		},
		{
			key: 'exercisePrice',
			header: '행사가',
			align: 'right',
			priority: 'high',
			cell: (row) => (row.exercisePrice === null ? '—' : `${formatDecimal(row.exercisePrice, 0)}원`)
		},
		{
			key: 'exerciseQty',
			header: '행사주수',
			align: 'right',
			priority: 'high',
			cell: (row) => (row.exerciseQty === null ? '—' : formatShares(Math.round(row.exerciseQty)))
		},
		{
			key: 'listDt',
			header: '상장일',
			align: 'center',
			priority: 'low',
			cell: (row) => dateOrDash(row.listDt)
		}
	];
}

export default function StockCbExercises({ rows }: { rows: CbExerciseRow[] })
{
	return (
		<DataTable<CbExerciseRow>
			caption='CB/BW 행사 내역 — 행사일, 채권, 행사가, 행사주수, 상장일'
			columns={buildColumns()}
			rows={rows}
			rowKey={(row, index) => `${row.bondIsin}-${row.rgtStdDt}-${index}`}
			emptyMessage='수집된 CB/BW 행사 내역이 없습니다.'
		/>
	);
}
